package com.ethink.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ethink.order.api.request.JkyTradeLogUpdateCkFlagRequest;
import com.ethink.order.api.request.JkyTradeLogUpdateErrorRequest;
import com.ethink.order.api.response.JkyTradeLogGetDetailResponse;
import com.ethink.order.common.entity.JkyTradeLogDetail;
import com.ethink.order.common.mapper.JkyTradeLogDetailMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ethink.order.api.info.JkyTradeLogInfo;
import com.ethink.order.api.request.JkyTradeLogAddRequest;
import com.ethink.order.api.request.JkyTradeLogUpdateRequest;
import com.ethink.order.api.response.JkyTradeLogAddResponse;
import com.ethink.order.common.entity.JkyTradeLog;
import com.ethink.order.common.mapper.JkyTradeLogMapper;
import com.ethink.order.converter.JkyTradeLogConverter;
import com.ethink.order.service.JkyTradeLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 吉客云销售单 Service接口实现
 *
 * @author default
 * @date 2022-11-25
 */
@Slf4j
@Service
public class JkyTradeLogServiceImpl implements JkyTradeLogService {

    @Autowired
    private JkyTradeLogMapper jkyTradeLogMapper;

    @Autowired
    private JkyTradeLogDetailMapper jkyTradeLogDetailMapper;

    @Autowired
    private JkyTradeLogConverter jkyTradeLogConverter;


    @Override
    public JkyTradeLogInfo getJkyTradeLogByTradeNo(String tradeNo) {
        return jkyTradeLogMapper.getJkyTradeLogByTradeNo(tradeNo);
    }

    @Override
    public JkyTradeLogInfo lastGmtModifiedJkyTradeLog() {
        return jkyTradeLogMapper.lastGmtModifiedJkyTradeLog();
    }

    @Override
    public JkyTradeLogInfo lastConsignTimeJkyTradeLog() {
        return jkyTradeLogMapper.lastConsignTimeJkyTradeLog();
    }

    @Override
    public JkyTradeLogGetDetailResponse topOneProcessingJkyTradeLog() {
        JkyTradeLogGetDetailResponse response = new JkyTradeLogGetDetailResponse();
        JkyTradeLogInfo jkyTradeLogInfo = jkyTradeLogMapper.topOneProcessingJkyTradeLog();
        if (jkyTradeLogInfo == null) {
            return null;
        }
        response.setJkyTradeLogInfo(jkyTradeLogInfo);
        response.setJkyTradeLogDetailInfoList(jkyTradeLogDetailMapper.queryJkyTradeLogDetailByLogId(jkyTradeLogInfo.getId()));
        return response;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public JkyTradeLogAddResponse addJkyTradeLog(JkyTradeLogAddRequest request) {
        JkyTradeLogAddResponse response = new JkyTradeLogAddResponse();
        JkyTradeLog jkyTradeLog = new JkyTradeLog();
        BeanUtils.copyProperties(request, jkyTradeLog);
        jkyTradeLogMapper.insert(jkyTradeLog);
        // 商品明细
        if (CollectionUtils.isNotEmpty(request.getItems())) {
            for (JkyTradeLogAddRequest.JkyTradeLogAddRequestMateItem mateItem : request.getItems()) {
                JkyTradeLogDetail jkyTradeLogDetail = new JkyTradeLogDetail();
                BeanUtils.copyProperties(mateItem, jkyTradeLogDetail);
                jkyTradeLogDetail.setLogId(jkyTradeLog.getId());
                jkyTradeLogDetailMapper.insert(jkyTradeLogDetail);
            }
        }
        response.setId(jkyTradeLog.getId());
        return response;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateJkyTradeLog(JkyTradeLogUpdateRequest request) {
        LambdaUpdateWrapper<JkyTradeLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JkyTradeLog::getId, request.getId());
        updateWrapper.set(JkyTradeLog::getTradeNo, request.getTradeNo());
        updateWrapper.set(JkyTradeLog::getTradeStatus, request.getTradeStatus());
        updateWrapper.set(JkyTradeLog::getCkFlag, request.getCkFlag());
        updateWrapper.set(JkyTradeLog::getCkTime, request.getCkTime());
        updateWrapper.set(JkyTradeLog::getWarehouseCode, request.getWarehouseCode());
        updateWrapper.set(JkyTradeLog::getWarehouseName, request.getWarehouseName());
        updateWrapper.set(JkyTradeLog::getGmtCreate, request.getGmtCreate());
        updateWrapper.set(JkyTradeLog::getGmtModified, request.getGmtModified());
        updateWrapper.set(JkyTradeLog::getConsignTime, request.getConsignTime());
        updateWrapper.set(JkyTradeLog::getChargeCurrency, request.getChargeCurrency());
        updateWrapper.set(JkyTradeLog::getShopId, request.getShopId());
        updateWrapper.set(JkyTradeLog::getShopName, request.getShopName());
        updateWrapper.set(JkyTradeLog::getOnlineTradeNo, request.getOnlineTradeNo());
        updateWrapper.set(JkyTradeLog::getTradeTime, request.getTradeTime());
        jkyTradeLogMapper.update(null, updateWrapper);
        // 根据主表ID，删除历史数据，然后新增
        // 商品明细删除
        LambdaQueryWrapper<JkyTradeLogDetail> detailQueryWrapper = new LambdaQueryWrapper<>();
        detailQueryWrapper.eq(JkyTradeLogDetail::getLogId, request.getId());
        // 商品明细新增
        if (CollectionUtils.isNotEmpty(request.getItems())) {
            for (JkyTradeLogUpdateRequest.JkyTradeLogUpdateRequestMateItem mateItem : request.getItems()) {
                JkyTradeLogDetail jkyTradeLogDetail = new JkyTradeLogDetail();
                BeanUtils.copyProperties(mateItem, jkyTradeLogDetail);
                jkyTradeLogDetail.setLogId(request.getId());
                jkyTradeLogDetailMapper.insert(jkyTradeLogDetail);
            }
        }
    }

    @Override
    public void updateJkyTradeLogCkFlag(JkyTradeLogUpdateCkFlagRequest request) {
        LambdaUpdateWrapper<JkyTradeLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JkyTradeLog::getId, request.getId());
        updateWrapper.set(JkyTradeLog::getCkFlag, request.getCkFlag());
        if (request.getCkFlag()) {
            updateWrapper.set(JkyTradeLog::getCkTime, new Date());
        }
        updateWrapper.set(JkyTradeLog::getIsError, request.getIsError());
        updateWrapper.set(JkyTradeLog::getRemark, request.getRemark());
        jkyTradeLogMapper.update(null, updateWrapper);
    }

    @Override
    public void updateJkyTradeLogError(JkyTradeLogUpdateErrorRequest request) {
        LambdaUpdateWrapper<JkyTradeLog> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(JkyTradeLog::getId, request.getId());
        updateWrapper.set(JkyTradeLog::getIsError, true);
        updateWrapper.set(JkyTradeLog::getRemark, request.getErrorMsg());
        jkyTradeLogMapper.update(null, updateWrapper);
    }

}
